<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class BlogController extends Controller
{
    //博文回复列表
    public function getList(Request $request)
    {
    	$blogreply=DB::table('blogreply')->
            orderBy('id','desc')->
            where(function($query)use($request){
                $keyword=$request->input('keyword');
                if($keyword){
                    $query->where('replyname','like','%'.$keyword.'%');
                }
            })
            ->paginate($request->input('num',10));
    	return view('admin.blogreply.list',['blogreply'=>$blogreply,'request'=>$request]);
    }

    //博文回复编辑
    public function getEdit(Request $request)
    {
    	$id=$request->input('id');
        $blogreply=DB::table('blogreply')->where('id',$id)->first();
        return view('admin.blogreply.edit',['blogreply'=>$blogreply]);
    }

    //博文回复更新
    public function postUpdate(Request $request)
    {
        
        //获取参数
        $data=$request->except(['_token','id']);
        //数据插入
        $res=DB::table('blogreply')->where('id',$request->input('id'))->update($data);
        if($res){
            return redirect('blogreply/list')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    //博文回复删除
    public function getDrop(Request $request) 
    {
        $id=$request->all();
        //输出id
        // dd($id);
        $res=DB::table('blogreply')->where('id',$id)->delete();
        if($res){
            return redirect('blogreply/list')->with('info','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    //博文回复添加
    public function getAdd()
    {
    	return view('admin.blogreply.add');
    }

    //添加回复插入
    public function postInsert(Request $request)
    {
        $data=$request->except('_token');
        // dd($data);
         $pid=$data['pid'];
        // dd($pid);
        //获取所要插入的回复内容
        // $blogreply=$request->only('replyname','content');
        // dd($blogreply);
        // dd($comment);
        // dd($id);
         $res=DB::table('blogreply')->where('pid','=',$pid)->first();
        // dd($res);
        $data['time']=time();
        // dd($data);
        if($res){
            $res2=DB::table('blogreply')->insert($data);
            return redirect('blogreply/list')->with('info','更新成功');
        }else{
            return back()->with('info','博文不存在,不能添加回复');
        }
    }
}
